package com.sunkyc.module.oss.client.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import com.sunkyc.module.oss.client.FileService;
import com.sunkyc.module.oss.entity.SysOss;
import io.github.artislong.core.minio.MinioOssClient;
import io.github.artislong.core.minio.model.MinioOssConfig;
import io.github.artislong.model.OssInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
public class MinIoOssFileService implements FileService {


    @Autowired
    @Qualifier("sunkycMinIoClient")
    private MinioOssClient minioOssClient;


    @Override
    public void delete(List<SysOss> list) {
        list.stream().map(sysOss -> sysOss.getFilePathPrifex()+sysOss.getFileName()+"."+sysOss.getFileFomat())
                        .forEach(item -> minioOssClient.delete(item));
    }

    @Override
    public SysOss upload(MultipartFile multipartFile) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        Map<String, String> map = initFileName(multipartFile.getOriginalFilename());
        SysOss oss = upload(inputStream, map.get("uploadName"));
        return toSysOss(map,oss);
    }

    @Override
    public SysOss upload(File file) throws FileNotFoundException {
        InputStream fileInputStream = new FileInputStream(file);
        Map<String, String> map = initFileName(file.getName());
        SysOss oss = upload(fileInputStream, map.get("uploadName"));
        return toSysOss(map,oss);
    }


    private SysOss toSysOss(Map<String,String> map,SysOss oss){
        oss.setFileOssUrl(map.get("endpoint")
                + "/" + map.get("bucketName") + "/"
                + map.get("uploadName"));
        oss.setFileName(map.get("name"));
        oss.setFilePathPrifex(map.get("prifex"));
        oss.setFileFomat(map.get("typeName"));
        return oss;
    }



    @Override
    public SysOss upload(InputStream inputStream, String name) {
        OssInfo ossInfo = upLoad(inputStream, name, false);
        SysOss oss = new SysOss();
        oss.setFileSize(ossInfo.getLength());
        oss.setFileName(ossInfo.getName());
        return oss;
    }

    private OssInfo upLoad(InputStream inputStream, String name, boolean isOverride) {
        return minioOssClient.upLoad(inputStream, name, isOverride);
    }


    private Map<String,String> initFileName(String fileName){
        String name = IdUtil.getSnowflakeNextIdStr();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd/");
        String prifex = LocalDateTimeUtil.format(LocalDate.now(), dateTimeFormatter);
        String typeName = FileNameUtil.extName(fileName);
        MinioOssConfig minioOssConfig = minioOssClient.getMinioOssConfig();
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        map.put("prifex",prifex);
        map.put("typeName",typeName);
        map.put("uploadName",prifex + name + "." + typeName);
        map.put("endpoint",minioOssConfig.getEndpoint());
        map.put("bucketName",minioOssConfig.getBucketName());
        return map;
    }


}
